YEAR=$(shell echo `expr $(shell echo `date +"%Y"`) + 0`)
MONTH=$(shell echo `expr $(shell echo `date +"%m"`) + 0`)
DAY=$(shell echo `expr $(shell echo `date +"%d"`) + 0`)
HOUR=$(shell echo `expr $(shell echo `date +"%H"`) + 0`)
MINUTE=$(shell echo `expr $(shell echo `date +"%M"`) + 0`)
SECOND=$(shell echo `expr $(shell echo `date +"%S"`) + 0`)
DATE=$(shell echo `date +"%Y"`)$(shell echo `date +"%m"`)$(shell echo `date +"%d"`)
MAJOR_VERSION=1
MINOR_VERSION=0
PATCH_VERSION=4
VERSION=$(MAJOR_VERSION).$(MINOR_VERSION).$(PATCH_VERSION)
VERSION_FILE=version.h

ROOTFS_DIR ?= build
ifeq ($(ROOTFS_DIR), build)
SOFTWARE_DIR ?= build
else
SOFTWARE_DIR = $(ROOTFS_DIR)/software
endif
TARGET_TYPE ?= debug
TARGET_NAME ?= test_threadpool
TARGET = $(TARGET_NAME)_$(VERSION)_$(DATE)
#ifeq ($(TARGET_TYPE), debug)
#TARGET = $(TARGET_NAME)
#endif
TARGET_DIR = output
OBJDIR = build

CROSS_COMPILE := 

CC=$(CROSS_COMPILE)gcc
CXX=$(CROSS_COMPILE)g++
LD=$(CROSS_COMPILE)g++
STRIP=$(CROSS_COMPILE)strip

SRC_PATH=src

INCLUDES = -I$(SRC_PATH)
VPATH    =   $(SRC_PATH)
INCLUDES += -I.

#base
BASE_PATH = $(SRC_PATH)
VPATH += $(BASE_PATH)
VPATH += $(BASE_PATH)/utils
VPATH += $(BASE_PATH)/utils/net
VPATH += $(BASE_PATH)/utils/cJSON
VPATH += $(BASE_PATH)/ccu
INCLUDES += -I$(BASE_PATH)/utils
INCLUDES += -I$(BASE_PATH)/utils/net
INCLUDES += -I$(BASE_PATH)/utils/cJSON
INCLUDES += -I$(BASE_PATH)/sys
INCLUDES += -I$(BASE_PATH)/ccu
MAIN_OBJS += $(BASE_PATH)/utils/lock.o 
MAIN_OBJS += $(BASE_PATH)/utils/wait.o 
MAIN_OBJS += $(BASE_PATH)/utils/utils.o 
MAIN_OBJS += $(BASE_PATH)/utils/tool.o 
MAIN_OBJS += $(BASE_PATH)/utils/fifo.o
MAIN_OBJS += $(BASE_PATH)/utils/net/network.o
MAIN_OBJS += $(BASE_PATH)/utils/cJSON/cJSON.o
MAIN_OBJS += $(BASE_PATH)/main.o 

#elog
ELOG_PATH = $(SRC_PATH)/utils/elog
VPATH += $(ELOG_PATH)
VPATH += $(ELOG_PATH)/plugins
VPATH += $(ELOG_PATH)/plugins/file
VPATH += $(ELOG_PATH)/port
INCLUDES += -I$(ELOG_PATH)/inc
INCLUDES += -I$(ELOG_PATH)/plugins/file
MAIN_OBJS += $(ELOG_PATH)/elog.o
MAIN_OBJS += $(ELOG_PATH)/elog_utils.o
MAIN_OBJS += $(ELOG_PATH)/elog_async.o
MAIN_OBJS += $(ELOG_PATH)/plugins/file/elog_file.o
MAIN_OBJS += $(ELOG_PATH)/port/elog_port.o
MAIN_OBJS += $(ELOG_PATH)/port/elog_file_port.o
LDFLAGS +=

#config
CONFIG_PATH = $(SRC_PATH)/config
VPATH += $(CONFIG_PATH)
INCLUDES += -I$(CONFIG_PATH)
MAIN_OBJS += $(CONFIG_PATH)/uconfig.o
LDFLAGS +=

#ccu
CCU_PATH=$(SRC_PATH)/ccu
VPATH += $(CCU_PATH)
INCLUDES += -I$(CCU_PATH)
MAIN_OBJS += $(CCU_PATH)/ccu.o
MAIN_OBJS += $(CCU_PATH)/ccu_modules.o
LDFLAGS +=

#threadpool
THRADEPOOL_PATH=$(SRC_PATH)/threadpool
VPATH += $(THRADEPOOL_PATH)
INCLUDES += -I$(THRADEPOOL_PATH)
MAIN_OBJS += $(THRADEPOOL_PATH)/threadpool.o
LDFLAGS +=

CFLAGS += $(INCLUDES)
CFLAGS += $(MODULES)
CFLAGS += -g -Ofast
CXXFLAGS += $(INCLUDES)
CXXFLAGS += $(MODULES)
CXXFLAGS += -g 

LDFLAGS += -g 
LDFLAGS += -lpthread

OBJS = $(patsubst %, $(OBJDIR)/%, $(notdir $(MAIN_OBJS)))
TARGET_OUTPUT = $(patsubst %, $(TARGET_DIR)/%, $(notdir $(TARGET)))
                                                                                                                        
all:dir_create version_file debug_target release_target

release_target:$(OBJS)
	$(LD) $(OBJS) -o $(TARGET_OUTPUT)_g $(LDFLAGS)
	$(STRIP) --strip-unneeded -o $(TARGET_OUTPUT) $(TARGET_OUTPUT)_g
	md5sum $(TARGET_OUTPUT) | cut -d" " -f1 > $(TARGET_OUTPUT).md5
	rm -f $(TARGET_OUTPUT)_g

debug_target:$(OBJS)
	$(LD) $(OBJS) -o $(TARGET_NAME)_g $(LDFLAGS) 
	$(STRIP) --strip-unneeded -o $(TARGET_NAME) $(TARGET_NAME)_g

version_file:
	@echo  \
	"/*********************************************************\n"\
	"  This file is automatically generated, please do not modify it\n"\
	"**********************************************************/\n"\
	"#ifndef __APP_VERSION_H__\n"\
	"#define __APP_VERSION_H__\n"\
	"\n"\
	"#define APP_MAJOR_VERSION $(MAJOR_VERSION)\n"\
	"#define APP_MINOR_VERSION $(MINOR_VERSION)\n"\
	"#define APP_PATCH_VERSION $(PATCH_VERSION)\n"\
	"\n"\
	"#define APP_BUILD_YEAR  $(YEAR)\n"\
	"#define APP_BUILD_MONTH $(MONTH)\n"\
	"#define APP_BUILD_DAY   $(DAY)\n"\
	"#define APP_BUILD_HOUR  $(HOUR)\n"\
	"#define APP_BUILD_MIN   $(MINUTE)\n"\
	"#define APP_BUILD_SEC   $(SECOND)\n"\
	"\n"\
	"#endif /*__APP_VERSION_H__*/"  > $(VERSION_FILE)

define CREATE_DIR
	if [ ! -d $(1) ];\
	 	then\
    	mkdir -p $(1);\
	fi	
endef

dir_create:  
	@$(call CREATE_DIR,$(OBJDIR))
	@$(call CREATE_DIR,$(TARGET_DIR))

clean:
	rm -rf $(OBJDIR)
	rm -rf $(TARGET_DIR)
	rm -f $(TARGET_NAME) $(TARGET_NAME)_g

$(OBJDIR)/%.o:%.S
	@echo "CC $<"
	@$(CC) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.o:%.c
	@echo "CC $<"
	@$(CC) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.o:%.cpp
	@echo "CXX $<"
	@$(CXX) $(CXXFLAGS) -o $@ -c $< 


